//
// RootFinder
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.tool;

import java.io.*;
import net.jextra.crack.*;
import net.jextra.crack.type.*;

/**
 * A tool to assist in interpreting an Object's "root" directory.
 */
public class RootFinder
{
    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    /**
     * <p>
     * Get a relative directory to the given object via a ../.. "backward" path.
     * </p>
     * 
     * @throws IOException
     */
    public File getRoot( Object obj )
        throws IOException
    {
        return getRoot( obj, "" );
    }

    public File getRoot( Object obj, String extraPath )
        throws IOException
    {
        String pkg = obj.getClass().getName();
        StringBuilder rootPath = new StringBuilder( "" );

        // Count the number of "." in the package name to get number of dirs to backup.
        for ( int dot = pkg.indexOf( '.' ); dot >= 0; dot = pkg.indexOf( '.' ) )
        {
            pkg = pkg.substring( dot + 1 );
            rootPath.append( "../" );
        }

        // Add extra backups.
        if ( extraPath != null )
        {
            String extra = extraPath.replace( '\\', '.' ).replace( '/', '.' ).replace( "..", "." );
            
            if ( extra.startsWith( "." ) )
            {
                extra = extra.substring( 1 );
            }

            if ( extra.endsWith( "." ) )
            {
                extra = extra.substring( 0, extra.length() - 1 );
            }

            String[] dirs = extra.split( "\\." );

            for ( int i = 0; i < dirs.length; i++ )
            {
                rootPath.append( "../" );
            }
        }

        System.out.println( "rootPath=" + rootPath.toString() );
        return new File( CrackBuild.getInstance().getCrackContext().getBuildDir(), rootPath.toString() )
            .getCanonicalFile();
    }

    /**
     * <p>
     * Get the Object's package path. For example, if the Object is com.mycompany.foo.Foo, the package prefix is
     * "com/mycompany/foo/".
     * </p>
     * <p>
     * This can be very useful in creating {@link FileStrainer}s.
     * </p>
     */
    public String getPackagePrefix( Object obj )
    {
        return obj.getClass().getPackage().getName().replace( '.', '/' ) + "/";
    }
}
